package com.ygou.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygou.mapper.CartMapper;
import com.ygou.mapper.GoodsMapper;
import com.ygou.mapper.SkusMapper;
import com.ygou.mapper.UsersMapper;
import com.ygou.pojo.Cart;
import com.ygou.pojo.CartExample;
import com.ygou.pojo.CartExample.Criteria;
import com.ygou.pojo.GoodsWithBLOBs;
import com.ygou.pojo.Skus;
import com.ygou.pojo.Users;
import com.ygou.pojo.UsersExample;
import com.ygou.service.CartService;

@Service
public class CartServiceImpl implements CartService {
	@Autowired
	private CartMapper cartMapper;
	@Override
	public boolean addCart(Cart cart) {
		int result=0;
		CartExample example=new CartExample();
		Criteria ci=example.createCriteria();
		ci.andUseridEqualTo(cart.getUserid());
		ci.andGoodsidEqualTo(cart.getGoodsid());
		ci.andSkuattrEqualTo(cart.getSkuattr());
		List<Cart> carts =cartMapper.selectByExample(example);
		Cart c=carts.size()==0?null:carts.get(0);
		if(c!=null){
			c.setGoodscount(c.getGoodscount()+cart.getGoodscount());
			c.setGoodstotalprice(c.getGoodsprice().multiply(new BigDecimal(c.getGoodscount())));
			c.setLasttime(new java.util.Date());
			result+=cartMapper.updateByPrimaryKey(c);
		}else{
			cart.setGoodstotalprice(cart.getGoodsprice().multiply(new BigDecimal(cart.getGoodscount())));
			result+=cartMapper.insert(cart);
		}
		return result>0;
	}
	@Override
	public List<Cart> findCartByUId(Integer userid) {
		CartExample example=new CartExample();
		Criteria c=example.createCriteria();
		c.andUseridEqualTo(userid);
		return cartMapper.selectByExample(example);
	}
	/**
	 * 修改购物车信息
	 */
	@Override
	public boolean updCartInfo(Cart cart) {
		return cartMapper.updateByPrimaryKey(cart)>0;
	}
	@Override
	public Cart selectByPrimaryKey(Integer cartid) {
		return cartMapper.selectByPrimaryKey(cartid);
	}
	@Override
	public boolean delCart(List<Integer> cartids) {
		CartExample example=new CartExample();
		Criteria c=example.createCriteria();
		c.andCartidIn(cartids);
		return cartMapper.deleteByExample(example)>0;
	}
	
}
